﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Things.Common;

namespace Things.Web.Components
{
    public partial class GoodsOrderAO : System.Web.UI.UserControl
    {
        public Guid GoodsOrderID
        {
            get
            {
                string temp = hfGoodsOrderID.Value;
                Guid guid = Guid.Empty;
                if (Guid.TryParse(temp, out guid) == false)
                {
                    guid = Guid.Empty;
                }
                return guid;
            }
            set
            {
                hfGoodsOrderID.Value = value.ToString();
            }
        }

        protected Things.BLL.GoodsOrders goBLL = new BLL.GoodsOrders();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Things.Model.GoodsOrders GOModel=goBLL.GetModel(GoodsOrderID);
                Visibility(GOModel);
            }
        }

        protected void Visibility(Things.Model.GoodsOrders GOModel)
        {
            if (GOModel != null)
            {
                // 交易成功
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已签收.ToString())
                {
                    lbtSuccess.Visible = true;
                }
                // 换货
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请换货.ToString())
                {
                    lbtHuanHuo.Visible = true;
                }
                // 退货确认
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请退货.ToString())
                {
                    lbtTuiHuo.Visible = true;
                }
                // 交易关闭
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请退货.ToString())
                {
                    lbtCloseOrder.Visible = true;
                }
                // 交易关闭
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已取消.ToString())
                {
                    lbtCloseOrder.Visible = true;
                }

                // 改价
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.未付款.ToString())
                {
                    LbtGaiJia.Visible = true;
                }

                // 发货
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已付款.ToString())
                {
                    lbtFaHuo.Visible = true;
                }

            }
        }

        protected void lbtSuccess_Click(object sender, EventArgs e)
        {
            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);
            if (GOModel != null)
            {
                // 签收后， 交易成功
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已签收.ToString())
                {
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.交易成功.ToString();
                    goBLL.Update(GOModel);
                }
            }
        }

        protected void lbtHuanHuo_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (string.IsNullOrEmpty(ddlKuaiDiGongSi.SelectedValue) == true)
            {
                strErr += "请选择物流公司！\\n";
            }
            if (this.txtWuLiuDanHao.Text.Trim().Length == 0)
            {
                strErr += "请输入快递单号！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }

            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);

            // 只有物流返回的信息是没有签收后，才可以换货
            if (GOModel != null)
            {
                // *** 待定：物流接口返回信息 ***
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请换货.ToString())
                {
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.交易关闭.ToString();
                    if (goBLL.Update(GOModel) == true)
                    {
                        GOModel.GoodsOrdersID = Guid.NewGuid();
                        GOModel.SendDate = DateTime.Now;
                        GOModel.KuaiDiGongSi = ddlKuaiDiGongSi.SelectedValue;
                        GOModel.KuaiDiDanHao = txtWuLiuDanHao.Text.Trim();
                        GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.已发货.ToString();
                        goBLL.Add(GOModel);
                    }
                }
            }
        }

        protected void lbtTuiHuo_Click(object sender, EventArgs e)
        {
            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);
            // 只有物流返回的信息是没有签收后，才可以退货
            if (GOModel != null)
            {
                // *** 待定：物流接口返回信息 ***
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请退货.ToString())
                {
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.交易关闭.ToString();
                    goBLL.Update(GOModel);
                }
            }
        }

        protected void LbtGaiJia_Click(object sender, EventArgs e)
        {
            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);

            if (GOModel != null)
            {
                // 未付款状态下，才可以改价
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.未付款.ToString())
                {
                    decimal price = GOModel.TotalPrice;
                    if (decimal.TryParse(txtPrice.Text.Trim(), out price) == true)
                    {
                        GOModel.TotalPrice = price;
                        goBLL.Update(GOModel);
                    }
                }
            }
        }

        protected void lbtCloseOrder_Click(object sender, EventArgs e)
        {
            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);

            if (GOModel != null)
            {
                // 确认退货后，交易关闭
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.申请退货.ToString())
                {
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.交易关闭.ToString();
                    goBLL.Update(GOModel);
                }

                // 取消订单后，交易关闭
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已取消.ToString())
                {
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.交易关闭.ToString();
                    goBLL.Update(GOModel);
                }
            }
        }

        protected void lbtFaHuo_Click(object sender, EventArgs e)
        {
            
            string strErr = "";
            if (string.IsNullOrEmpty(ddlKuaiDiGongSi.SelectedValue) == true)
            {
                strErr += "请选择物流公司！\\n";
            }
            if (this.txtWuLiuDanHao.Text.Trim().Length == 0)
            {
                strErr += "请输入快递单号！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this.Page, strErr);
                return;
            }

            Things.Model.GoodsOrders GOModel = goBLL.GetModel(GoodsOrderID);

            // 只有已付款后，才可以发货
            if (GOModel != null)
            {
                if (GOModel.Status == Things.Common.ConstantString.GoodsOrderSatus.已付款.ToString())
                {
                    GOModel.KuaiDiGongSi = ddlKuaiDiGongSi.SelectedValue;
                    GOModel.KuaiDiDanHao = txtWuLiuDanHao.Text.Trim();
                    GOModel.Status = Things.Common.ConstantString.GoodsOrderSatus.已发货.ToString();
                    goBLL.Update(GOModel);
                }
            }
        }
    }
}